package Threads.CAS;

import java.util.concurrent.atomic.AtomicReference;

public class ABA {
    static AtomicReference<String> stringAtomicReference=new AtomicReference<>("A");
    public static void main(String[] args) throws InterruptedException {
        String s = stringAtomicReference.get();
        Change(stringAtomicReference);
        Thread.sleep(1000);

        //这里并没有发现，所以返回的仍然是true
        boolean b = stringAtomicReference.compareAndSet("A", "C");
        System.out.println(b);
    }

    //两个线程，先是该变量的值为，又改成A
    public static void Change(AtomicReference<String> atomicReference) throws InterruptedException {
        new Thread(()->{
            atomicReference.compareAndSet("A","B");
        }).start();
        Thread.sleep(500);
        new Thread(()->{
            atomicReference.compareAndSet("B","A");
        }).start();
    }
}
